import { tokenKey } from '@/utils/config'
import utils from '@/utils/utils'
import { parseRoute, redirect} from '@/utils'

export function openLoginDialog() {
  const userToken = uni.getStorageSync(tokenKey)
  if (!userToken) {
    uni.navigateTo({
      url: '/pages/auth/login'
    })
  }
}

export function redirectTo(params = {}) {
  const pages = getCurrentPages() // 获取加载的页面
  const currentPage = pages[pages.length - 1] // 获取当前页面的对象
  const options = currentPage.options // 如果要获取url中所带的参数可以查看options
  params = Object.assign(params, options)
  const url = '/' + redirect(params)
  // uni.reLaunch({ url: url })
  uni.navigateTo({ url: url })
}

export function authLoginTo() {
	utils.clear()
  const pages = getCurrentPages() // 获取加载的页面
  const currentPage = pages[pages.length - 1] // 获取当前页面的对象
  const route = currentPage.route // 当前页面url
  const options = currentPage.options // 如果要获取url中所带的参数可以查看options
  const params = { ...options }
  params.redirect = route
  const url = parseRoute({
    path: '/pages/login/login',
    params: params
  })
  uni.navigateTo({
    url: url
  })
}